<template>
  <div class="app-container">
    <!-- 筛选框 -->
    <el-form :inline="true">
      <el-form-item>
        <el-input v-model="search_form.adName" name="adName" placeholder="请输入新鲜事名称" />
      </el-form-item>
      <el-form-item>
        <el-select v-model="search_form.status" placeholder="请选择上架状态">
          <el-option label="全部" value="" />
          <el-option label="上架" value="1" />
          <el-option label="下架" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-cascader
          v-model="search_form.provinceId"
          placeholder="请选择省份"
          :options="addressOptions"
          :props="propOption"
          @change="handleChange"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain class="el-icon-search" @click="search">查询</el-button>
        <el-button type="primary" plain class="el-icon-refresh" @click="reset">重置</el-button>
      </el-form-item>
      <el-form-item style="float:right">
        <el-button
          type="primary"
          plain
          class="el-icon-plus"
          @click="showMask"
        >添加</el-button>
      </el-form-item>
    </el-form>

    <!-- 新鲜事列表 -->
    <div class="tableWrap">
      <el-table :data="tableData" border style="width: 100%">
        <!-- id -->
        <el-table-column prop="sort" label="排序" align="center" />
        <!-- 项目名称 -->
        <el-table-column prop="adName" label="项目名称" align="center" />
        <!-- 项目名称 -->
        <el-table-column label="项目图片" align="center">
          <template slot-scope="scope">
            <div>
              <img :src="common.img_url+scope.row.adPicture" alt class="smallBG">
            </div>
          </template>
        </el-table-column>

        <!-- 省份 -->
        <el-table-column prop="province" label="省份" align="center" />

        <!-- 上架状态 -->
        <el-table-column label="上架状态" align="center">
          <template slot-scope="scope">
            <div>
              <el-switch
                v-model="scope.row.status"
                active-text="上架"
                inactive-text="下架"
                :active-value="1"
                :inactive-value="0"
                @change="changeSwitch($event,scope.row)"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="jumpUrl" :formatter="jumpFormat " label="跳转链接" align="center" />
        <!-- 操作 -->
        <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="editAdv(scope.row)">编辑</el-button>
            <el-button type="text" size="mini" @click="deleteAdv(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    </div>

    <!-- 添加的页面 -->
    <adv-add
      v-if="showAddMask"
      :is-type="type"
      :is-hidden="showAddMask"
      :detail-info="detailInfo"
      :is-disabled="isDisabled"
      :address="addressOptions"
      @checkMask="checkMask"
      @refresh="addOrUpdate"
    />

    <!-- 分页器 -->
    <div class="block">
      <el-pagination
        background
        :page-sizes="[10,20,50,100]"
        :page-size="pageSize"
        :pager-count="11"
        layout="total, sizes, prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>

  </div>
</template>

<script>
  import advAdd from '@/components/adv/adv_add'
  import { provinceList } from '../../api/province'
  import { deleteAd, getAdList, updateAd } from '../../api/adv' // 添加组件
  export default {
    components: {
      advAdd
    },
    data() { // 数据声明初始
      return {
        adName: null,
        status: null,
        addressOptions: [], // 省份选择 options
        propOption: {
          value: 'id',
          label: 'name'
        }, // 省份选择 props
        search_form: {
          adName: null,
          status: null,
          provinceId: []
        }, // 筛选条件
        provinceId: [], // 省份id？
        type: 7, // 7-新鲜事
        showAddMask: false, // 是否显示添加弹窗
        page: 1,
        pageSize: 10,
        total: 0,
        tableData: [], // 列表数据
        detailInfo: {}, // 详细信息-修改
        isDisabled: false, // 默认允许是否编辑
        table_loading: false
      }
    },
    // 生命周期钩子，初始时获取数据
    mounted() {
      // 获取数据列表
      this.getFreshLists()
      // 获取省option
      provinceList({}).then(res => {
        this.addressOptions = res.data.data.list // 将获取的省份option 赋值 addressOptions
      }).catch(res => {
        this.$message.error('网络连接错误，请稍后再试')
      })
    },
    methods: {

      jumpFormat(row, column) {
        if (row.jumpUrl === 'card:1') {
          return '会员卡'
        }else if (row.jumpUrl.indexOf('id:') === 0) {
          return '项目详情/'+row.jumpUrl
        }else {
          return row.jumpUrl
        }
      },
      //
      checkMask(isHidden) {
        this.detailInfo = {}
        this.showAddMask = isHidden
      },

      addOrUpdate() {
        // 刷新列表
        this.getFreshLists({
          adName: this.search_form.adName ? this.search_form.adName : null,
          status: parseInt(this.search_form.status) === 0 ? 0 : parseInt(this.search_form.status),
          provinceId: null,
          pageSize: this.pageSize,
          pageNum: this.page,
          positionType: this.type
        })
      },

      // 筛选
      search() {
        this.getFreshLists({
          adName: this.search_form.adName ? this.search_form.adName : null,
          status: parseInt(this.search_form.status) === 0 ? 0 : parseInt(this.search_form.status),
          provinceId: this.search_form.provinceId[0],
          pageSize: this.pageSize,
          pageNum: 1,
          positionType: this.type
        })
      },
      // 重置
      reset() {
        this.page = 1
        this.pageSize = 10
        this.search_form.adName = ''
        this.search_form.status = ''
        this.search_form.provinceId = []
        this.getFreshLists()
      },
      // 获取数据列表
      getFreshLists(options = {
        pageSize: this.pageSize,
        pageNum: this.page,
        positionType: this.type
      }) {
        this.table_loading = true
        getAdList(options).then(res => {
          this.total = res.data.data.count
          this.pageNum = res.data.data.pageNum
          this.pageSize = res.data.data.pageSize
          this.tableData = res.data.data.list
          this.table_loading = false
        }).catch((error) => {
          this.table_loading = false
          this.$message.error('网络连接错误，请稍后再试')
        })
      },

      // 上架状态改变
      changeSwitch(status, item) {
        updateAd({
          id: item.id,
          status: status
        }).then(res => {
          if (res.data.code === 0) {
            this.$message({
              message: '状态修改成功',
              type: 'success'
            })
          } else {
            this.$message.error(res.data.msg)
          }
          // 刷新列表
          this.getFreshLists({
            adName: this.search_form.adName ? this.search_form.adName : null,
            status: parseInt(this.search_form.status) === 0 ? 0 : parseInt(this.search_form.status),
            provinceId: this.search_form.provinceId[0],
            pageSize: this.pageSize,
            pageNum: this.page,
            positionType: this.type
          })
        }).catch(err => {
          this.$message.error(err)
        })
      },
      // 添加
      showMask() {
        this.detailInfo = {} // 详细信息初始为空
        this.showAddMask = true // 显示添加弹窗
      },
      // 删除
      deleteAdv(id) {
        this.$confirm('确认删除该新鲜事吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteAd({
            id: id
          }).then((result) => {
            const res = result.data
            if (res.code === 0) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getFreshLists({
                adName: this.search_form.adName ? this.search_form.adName : null,
                status: parseInt(this.search_form.status) === 0 ? 0 : parseInt(this.search_form.status),
                provinceId: null,
                pageSize: this.pageSize,
                pageNum: this.page,
                positionType: this.type
              })
            } else {
              this.$message.error(res.msg)
            }
          }).catch((err) => {
            this.$message.error('服务器网络错误，请稍后重试')
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      // 编辑
      editAdv(row) {
        this.detailInfo = row // 赋值
        this.isDisabled = false // 能否编辑
        this.showAddMask = true // 显示添加弹窗
      },

      // 省份选择器change事件
      handleChange(val) {
        if (val.lenght > 0) {
          this.provinceId = val[0]
        }
      },

      // 当前页面改变
      handleCurrentChange(val) {
        this.getFreshLists({
          adName: this.search_form.adName ? this.search_form.adName : null,
          status: parseInt(this.search_form.status) === 0 ? 0 : parseInt(this.search_form.status),
          provinceId: this.search_form.provinceId[0],
          pageSize: this.pageSize,
          pageNum: val,
          positionType: this.type
        })
      },

      // 页数改变
      handleSizeChange(page) {
        this.getFreshLists({
          adName: this.search_form.adName ? this.search_form.adName : null,
          status: parseInt(this.search_form.status) === 0 ? 0 : parseInt(this.search_form.status),
          provinceId: this.search_form.provinceId[0],
          'pageSize': page,
          'pageNum': this.page,
          positionType: this.type
        })
      }

    }
  }
</script>

<style scoped>
  label {
    font-size: 14px;
    font-weight: normal;
  }
  .smallBG {
    width: 100px;
    height: 100px;
  }
  .tableWrap {
    margin-top: 20px;
  }
  .block {
    background: #fff;
    padding: 32px 16px;
  }
</style>
